<?php
class Cont_PagDAO extends GenericDAO  {

	function __construct() {
		$this->tabela["alteracao"] = array(	"id" 	=> 470,
											"nome" 	=> strtolower(substr(get_class(), 0, -3)));
	
		$this->tabela["consulta"]  = array(	"id" 	=> 479,
											"nome" 	=> "vw_" . strtolower(substr(get_class(), 0, -3)));
	}
	
	public function consultarPK($valor) {
	    $sql = "where id = :id";
	    $params['id'] = $valor;
	    return $this->consultar($sql, $params);
	}
	
	public function inserir($registro, $finalizarTransacao = true) {
		
		$data = explode('/',$registro['data']);
		if (isset($data[2])) {
			$registro['data'] = $data[2] . "-" . $data[1] . "-" . $data[0];
		} else {
			throw new Exception('Data não informada!');
		}
		
		$this->inserirRegistro($registro, $finalizarTransacao);
		$registro['id'] = $this->ultimoId();
		return $registro;
	}
	
	public function excluir($id, $finalizarTransacao = true) {
		$pk['id'] = $id;
		return $this->excluirRegistro($pk, $finalizarTransacao);	
	}
	
	public function alterar($registro, $finalizarTransacao = true) {
		$pk['id'] 		= $registro['id'];
		unset($registro['id']);

		return $this->alterarRegistro($registro, $pk, $finalizarTransacao);
	}
	
	public function consultarRelatorio($tipo, $dt_ini, $dt_fim) {
		$where = "where ";
		$params = null;
		switch ($tipo) {
			case "ap":
				$where .= "apagar <> '0,00' and dt_data between :dt_ini and :dt_fim ";
				$data = explode("/", $dt_ini);
				$params['dt_ini'] = $data[2] . "-" . $data[1] . "-" . $data[0]; 
				$data = explode("/", $dt_fim);
				$params['dt_fim'] = $data[2] . "-" . $data[1] . "-" . $data[0]; 
				break;
			case "pg":
				$where .= "apagar = '0,00' and dt_data between :dt_ini and :dt_fim ";
				$data = explode("/", $dt_ini);
				$params['dt_ini'] = $data[2] . "-" . $data[1] . "-" . $data[0]; 
				$data = explode("/", $dt_fim);
				$params['dt_fim'] = $data[2] . "-" . $data[1] . "-" . $data[0]; 
				break;
			case "pp":
				$where .= "dt_data < :data and apagar <> '0,00' ";
				$params['data'] = date("Y-m-d");
				break;					
		}
		return $this->consultar($where, $params);
	}
}
?>